using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Catastrophe.Common.FileM;

namespace BaseAmd.Mmp.FileNameFormat
{
	public class BaseFileRename
	{
		public virtual FileInfoEntity FileNameFormat(string fullName, List<string> extName)
		{
			return FileUtil.GetFileInfoEntity(fullName, extName);
		}

		public virtual Tuple<bool, string, string, string> NameFormat(string name)
		{
			string text = (name.Contains(".") ? ("." + name.Split('.').Last((string x) => x.Trim() != "")) : "");
			string text2 = (text.StartsWith(".") ? text.Remove(0, 1) : text);
			string item = name.Replace(text, "");
			return new Tuple<bool, string, string, string>(item1: true, name.ToUpper(), item, text2.ToUpper());
		}

		public virtual Tuple<bool, string, string> NameFormatReverse(string name)
		{
			string text = (name.Contains(".") ? ("." + name.Split('.').Last((string x) => x.Trim() != "")) : "");
			return new Tuple<bool, string, string>(item1: true, name, text.ToUpper());
		}

		public virtual Tuple<string, string> RenameIdIncrease(ref string nameWithOutExtension, string extend, ref int id)
		{
			id++;
			string item = nameWithOutExtension + "." + extend.ToUpper();
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(3, 3);
			defaultInterpolatedStringHandler.AppendFormatted(nameWithOutExtension);
			defaultInterpolatedStringHandler.AppendLiteral("(");
			defaultInterpolatedStringHandler.AppendFormatted(id);
			defaultInterpolatedStringHandler.AppendLiteral(").");
			defaultInterpolatedStringHandler.AppendFormatted(extend.ToUpper());
			string item2 = defaultInterpolatedStringHandler.ToStringAndClear();
			return new Tuple<string, string>(item, item2);
		}

		public NcPathInfo NameFormat(string name, bool toUpper)
		{
			NcPathInfo ncPathInfo = new NcPathInfo();
			string[] array = null;
			char splitFlag = ' ';
			string pathFlag = "/";
			if (name.Contains("/"))
			{
				name = name.Replace("\\", "/");
				splitFlag = '/';
			}
			else if (name.Contains('\\'))
			{
				splitFlag = '\\';
			}
			else
			{
				pathFlag = "";
			}
			array = (from x in name.Split(splitFlag)
				where x.Trim() != "" && x.Trim() != splitFlag.ToString()
				select x).ToArray();
			Func<string, string> func = delegate(string value)
			{
				StringBuilder stringBuilder14 = new StringBuilder();
				if (value.StartsWith("_N_"))
				{
					StringBuilder stringBuilder15 = stringBuilder14;
					StringBuilder stringBuilder16 = stringBuilder15;
					StringBuilder.AppendInterpolatedStringHandler handler2 = new StringBuilder.AppendInterpolatedStringHandler(0, 2, stringBuilder15);
					handler2.AppendFormatted(pathFlag);
					handler2.AppendFormatted(value);
					stringBuilder16.Append(ref handler2);
				}
				else if (value.StartsWith("_"))
				{
					StringBuilder stringBuilder15 = stringBuilder14;
					StringBuilder stringBuilder17 = stringBuilder15;
					StringBuilder.AppendInterpolatedStringHandler handler2 = new StringBuilder.AppendInterpolatedStringHandler(2, 2, stringBuilder15);
					handler2.AppendFormatted(pathFlag);
					handler2.AppendLiteral("_N");
					handler2.AppendFormatted(value);
					stringBuilder17.Append(ref handler2);
				}
				else
				{
					StringBuilder stringBuilder15 = stringBuilder14;
					StringBuilder stringBuilder18 = stringBuilder15;
					StringBuilder.AppendInterpolatedStringHandler handler2 = new StringBuilder.AppendInterpolatedStringHandler(3, 2, stringBuilder15);
					handler2.AppendFormatted(pathFlag);
					handler2.AppendLiteral("_N_");
					handler2.AppendFormatted(value);
					stringBuilder18.Append(ref handler2);
				}
				return stringBuilder14.ToString();
			};
			Func<string, bool?> func2 = delegate(string value)
			{
				if (value.StartsWith("_N_"))
				{
					return true;
				}
				int num2 = value.LastIndexOf('_');
				int num3 = value.LastIndexOf('.');
				return (num2 == num3) ? null : new bool?((num2 > num3) ? true : false);
			};
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = new StringBuilder();
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				if (i == 0 && text.ToUpper() == "NC")
				{
					StringBuilder stringBuilder3 = stringBuilder;
					StringBuilder stringBuilder4 = stringBuilder3;
					StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(2, 1, stringBuilder3);
					handler.AppendFormatted(pathFlag);
					handler.AppendLiteral("NC");
					stringBuilder4.Append(ref handler);
					stringBuilder3 = stringBuilder2;
					StringBuilder stringBuilder5 = stringBuilder3;
					handler = new StringBuilder.AppendInterpolatedStringHandler(2, 2, stringBuilder3);
					handler.AppendFormatted(pathFlag);
					handler.AppendFormatted(pathFlag);
					handler.AppendLiteral("NC");
					stringBuilder5.Append(ref handler);
					continue;
				}
				bool? flag = func2(text);
				if (flag == true)
				{
					StringBuilder stringBuilder3 = stringBuilder;
					StringBuilder stringBuilder6 = stringBuilder3;
					StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(0, 2, stringBuilder3);
					handler.AppendFormatted(pathFlag);
					handler.AppendFormatted(text);
					stringBuilder6.Append(ref handler);
					string[] array2 = (from x in text.Split('_')
						where x.Trim() != "" && x.Trim().ToUpper() != "N"
						select x).ToArray();
					if (array2.Length == 0)
					{
						continue;
					}
					stringBuilder2.Append(pathFlag);
					for (int j = 0; j < array2.Length; j++)
					{
						string value2 = array2[j];
						if (j == array2.Length - 1)
						{
							stringBuilder3 = stringBuilder2;
							StringBuilder stringBuilder7 = stringBuilder3;
							handler = new StringBuilder.AppendInterpolatedStringHandler(0, 1, stringBuilder3);
							handler.AppendFormatted(value2);
							stringBuilder7.Append(ref handler);
						}
						else
						{
							stringBuilder3 = stringBuilder2;
							StringBuilder stringBuilder8 = stringBuilder3;
							handler = new StringBuilder.AppendInterpolatedStringHandler(1, 1, stringBuilder3);
							handler.AppendFormatted(value2);
							handler.AppendLiteral(".");
							stringBuilder8.Append(ref handler);
						}
					}
				}
				else if (flag == false)
				{
					int num = text.LastIndexOf('.');
					string arg = text.Substring(0, num);
					StringBuilder stringBuilder3 = stringBuilder;
					StringBuilder stringBuilder9 = stringBuilder3;
					StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(0, 1, stringBuilder3);
					handler.AppendFormatted(func(arg));
					stringBuilder9.Append(ref handler);
					string value3 = text.Substring(num + 1, text.Length - num - 1);
					stringBuilder3 = stringBuilder;
					StringBuilder stringBuilder10 = stringBuilder3;
					handler = new StringBuilder.AppendInterpolatedStringHandler(1, 1, stringBuilder3);
					handler.AppendLiteral("_");
					handler.AppendFormatted(value3);
					stringBuilder10.Append(ref handler);
					stringBuilder3 = stringBuilder2;
					StringBuilder stringBuilder11 = stringBuilder3;
					handler = new StringBuilder.AppendInterpolatedStringHandler(0, 2, stringBuilder3);
					handler.AppendFormatted(pathFlag);
					handler.AppendFormatted(text);
					stringBuilder11.Append(ref handler);
				}
				else
				{
					StringBuilder stringBuilder3 = stringBuilder2;
					StringBuilder stringBuilder12 = stringBuilder3;
					StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(0, 2, stringBuilder3);
					handler.AppendFormatted(pathFlag);
					handler.AppendFormatted(text);
					stringBuilder12.Append(ref handler);
					stringBuilder3 = stringBuilder;
					StringBuilder stringBuilder13 = stringBuilder3;
					handler = new StringBuilder.AppendInterpolatedStringHandler(0, 2, stringBuilder3);
					handler.AppendFormatted(pathFlag);
					handler.AppendFormatted(text);
					stringBuilder13.Append(ref handler);
				}
			}
			if (toUpper)
			{
				ncPathInfo.RealPath = stringBuilder.ToString().ToUpper();
				ncPathInfo.LogicPath = stringBuilder2.ToString().ToUpper();
			}
			else
			{
				ncPathInfo.RealPath = stringBuilder.ToString();
				ncPathInfo.LogicPath = stringBuilder2.ToString();
			}
			return ncPathInfo;
		}
	}
}
